package jdbc;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class adminshowcourse
 */
@WebServlet("/src/adminshowcourse")
public class adminshowcourse extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public adminshowcourse() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setHeader("Access-Control-Allow-Origin", "*");//模板
	    response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");//模板
	    response.setHeader("Access-Control-Allow-Headers", "Content-Type");//模板
	    //设置编码格式
	    request.setCharacterEncoding("UTF-8");//模板
	    response.setCharacterEncoding("UTF-8");//模板
		
	    String current_page = request.getParameter("current_page1");
	    String recordsperpage = request.getParameter("recordsperpage1");
	    String search = request.getParameter("search");
	    int currentPage = Integer.parseInt(current_page);
	    int recordsPerpage = Integer.parseInt(recordsperpage);
	    
	    hello a = new hello();//模板
		try {//模板
			ResultSet b;
			if (search!=null) {
				
				b = a.doselect("SELECT  "
						+ "    COUNT(*) AS total_records "
						+ "FROM  "
						+ "    course "
						+ "JOIN  "
						+ "    teacher ON course.course_teacher = teacher.teacher_id "
						+ "JOIN  "
						+ "    classroom ON course.course_classroom = classroom.classroom_id "
						+ "WHERE  "
						+ "    course_name LIKE ? "
						+ "    OR course_major LIKE ? "
						+ "    OR course_college LIKE ? "
						+ "    OR teacher.teacher_name LIKE ? "
						+ "    OR classroom.classroom_name LIKE ? "
						+ "    OR course_time LIKE ?; ","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%");
				b.next();
				response.getWriter().write(b.getString("total_records")+";");
				b = a.doselect("SELECT  "
						+ "    course_id, "
						+ "    course_name, "
						+ "    course_major, "
						+ "    course_college, "
						+ "    teacher.teacher_name, "
						+ "    course.classroom, "
						+ "    course_time "
						+ "FROM  "
						+ "    course "
						+ "JOIN  "
						+ "    teacher ON course.course_teacher = teacher.teacher_id "
						+ "JOIN  "
						+ "    classroom ON course.course_classroom = classroom.classroom_id "
						+ "WHERE  "
						+ "    course_name LIKE ? "
						+ "    OR course_major LIKE ? "
						+ "    OR course_college LIKE ? "
						+ "    OR teacher.teacher_name LIKE ? "
						+ "    OR classroom.classroom_name LIKE ? "
						+ "    OR course_time LIKE ?; "
						+ "LIMIT ?, ?","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%","%"+search+"%", (currentPage-1)*recordsPerpage, recordsPerpage);
			}
			else {
				b = a.doselect("SELECT COUNT(*) AS total_courses FROM course; ");
				b.next();
				response.getWriter().write(b.getString("total_courses")+";");

				b = a.doselect("SELECT  "
						+ "    course_id, "
						+ "    course_name, "
						+ "    course_major, "
						+ "    course_college, "
						+ "    teacher.teacher_name, "
						+ "    course_classroom, "
						+ "    course_time "
						+ "FROM  "
						+ "    course "
						+ "JOIN  "
						+ "    teacher ON course.course_teacher = teacher.teacher_id "
						+ "JOIN  "
						+ "    classroom ON course.course_classroom = classroom.classroom_id "
						+ "LIMIT ?, ?",(currentPage-1)*recordsPerpage, recordsPerpage);
			}
			while(b.next()) {//b代表查询结果，查询之后的那个表，next（）函数就是往下一行，逐行扫描
				response.getWriter().write(b.getString("course_name")+";");//这里表示的是往返回的那个response里面添加字符串，修改write括号后的内容即可
				response.getWriter().write(b.getString("course_major")+";");
				response.getWriter().write(b.getString("course_college")+";");
				response.getWriter().write(b.getString("teacher_name")+";");
				response.getWriter().write(b.getString("course_classroom")+";");
				response.getWriter().write(b.getString("course_time")+";");
				response.getWriter().write(b.getString("course_id")+";");
			}//模板
			b.close();
		
		} catch (SQLException e) {//模板
			e.printStackTrace();//模板
		}//模板
		}
}
